<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\api\model\v1\Knowledge;
use app\api\model\v1\KnowledgeFolder;
use app\BaseController;
use think\Request;

class KnowLedgeCon extends BaseController
{


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/22 16:52
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源
     */
    public function getKnowLedgeInfo(){
//        $limit = $this->request->param('limit',50);
        $limit = 50;
        $knowledge_folder_id = $this->request->param('knowledge_folder_id');
        $map = [];
        if (intval($knowledge_folder_id) > 0){
            if ($this->request->has('knowledge_folder_id') && !empty($this->request->param('knowledge_folder_id'))) {
                $map[] = ['knowledge_folder_id', '=', $this->request->param('knowledge_folder_id')];
            }
        }
        if ($this->request->has('title') && !empty($this->request->param('title'))) {
            $map[] = ['title', 'like', '%' . $this->request->param('title') . '%'];
        }
        $res = Knowledge::where($map)->order('create_time desc')->paginate($limit);
        return $this->response($res);
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/22 16:34
     * @return \think\response\Json
     * 上传文件
     */
    public function saveKnowLedge()
    {
        $data = $this->request->param();

        $data['admin_id'] = $this->request->uid;
        $data['domain_port'] = 'https://img.ssxsd.cn:998/ghpt_img';
        $data['suffix'] = substr($data['title'], strrpos($data['title'], ".") + 1) ?? '';
//        $data['path'] = $data['hash'] . '.' . $data['suffix'];
        Knowledge::create($data);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/22 17:47
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除文件
     */
    public function delKnowLedge($id){
        $res = Knowledge::find($id);
        if (is_null($res)){
            return $this->response(404,'未找到资源');
        }
        $res->delete();
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/22 17:40
     * @return \think\response\Json
     * 保存文件夹
     */
    public function saveKnowLedgeFolder(){
        $data = $this->request->param();
        $data['admin_id'] = $this->request->uid;
        KnowledgeFolder::create($data);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/22 17:46
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取列表
     */
    public function getKnowLedgeFolderList(){

        $res = KnowledgeFolder::select();
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/22 17:47
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除文件夹
     */
    public function delKnowLedgeFolder($id){
        $res = KnowledgeFolder::with('knowledge')->find($id);
        if (is_null($res)){
            return $this->response(404,'未找到资源');
        }
        $res->together(['knowledge'])->delete();
        return $this->response();
    }
}
